﻿/*
 * HyperStat.NET - The minimalist's static website compiler.
 * Copyright (C) 2012 Benjamin Brent
 * http://www.hyperstat.net/
 * http://hyperstat.codeplex.com/
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;

namespace HyperStat
{
    class Util
    {
        /// <summary>
        /// Changes the extension on a given filepath.
        /// </summary>
        /// <param name="path">The filepath to use.</param>
        /// <param name="newExt">The new extension, without a dot. For example: exe</param>
        /// <returns>The filepath with the new extension.</returns>
        public static string ChangeExtension(string path, string newExt)
        {
            string[] parts = path.Split('.');
            
            // Path already has an extension
            if (parts.Length > 1)
                path = String.Join(".", parts, 0, parts.Length - 1);

            // Path already has multiple extensions
            else if (parts.Length > 2)
                path = String.Join(".", parts, 0, parts.Length - 2);

            path += "." + newExt;

            return path;
        }

        /// <summary>
        /// Finds the extension from the given path.
        /// </summary>
        /// <param name="path">The path to use.</param>
        /// <returns>The extension, without a dot (e.g. exe) or empty if no extension.</returns>
        public static string GetExtension(string path)
        {
            string[] parts = path.Split('.');
            if (parts.Length > 1)
                return parts[parts.Length - 1];
            else
                return string.Empty;
        }

        /// <summary>
        /// Returns the given resource as a string.
        /// </summary>
        /// <param name="resource">The resource to return.</param>
        /// <param name="scope">The scope.</param>
        /// <returns>The resource, as a string.</returns>
        public static string ResourceAsString(string resource)
        {
            string[] resources = Assembly.GetExecutingAssembly().GetManifestResourceNames();
            if (resource != null && resources.Length > 0)
            {
                for (int i = 0; i < resources.Length; i++)
                {
                    if (!resources[i].Contains(resource, StringComparison.OrdinalIgnoreCase))
                        continue;
                    using (var resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resources[i]))
                        if (resourceStream != null)
                            using (var reader = new StreamReader(resourceStream))
                                if (reader != null)
                                    return reader.ReadToEnd();
                }
            }
            return null;
        }

        /// <summary>
        /// Replaces all slashes with the system-default slash in the given path.
        /// </summary>
        /// <param name="path">The path to be pathified.</param>
        /// <returns>The path with all slashes converted to the system's directory separator character.</returns>
        public static string Pathify(string path)
        {
            path = path.Replace(@"\", Path.DirectorySeparatorChar.ToString()).Replace(@"/", Path.DirectorySeparatorChar.ToString());
            return path;
        }

        /// <summary>
        /// Removes all trailing directory separator characters.
        /// </summary>
        public static string Unslashify(string path)
        {
            return path.TrimEnd('/').TrimEnd('\\').TrimEnd(Path.DirectorySeparatorChar);
        }

        /// <summary>
        /// Ensures that the given path ends with a trailing directory separator char.
        /// </summary>
        public static string Slashify(string path)
        {
            return Unslashify(path) + Path.DirectorySeparatorChar;
        }
    }
}
